﻿/******************************************************/
// SiteMaster プレゼンテーションクラス  
/******************************************************/

using System;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using MAA.Business;
using MAA.Entity;
using System.Drawing;

namespace MAA.Presentation
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            langHomePage.Text = rm.GetString("langHomePage");
            langNews.Text = rm.GetString("langNews");
            langRiceInfo.Text = rm.GetString("langRiceInfo");
            langTrading.Text = rm.GetString("langTrading");
            langWarehouse.Text = rm.GetString("langWarehouse");
            linkBtnLogin.Text = rm.GetString("langLogin");
            linkBtnLogout.Text = rm.GetString("langLogout");
            linkBtnRegister.Text = rm.GetString("langRegister");
            ImVN.ToolTip = rm.GetString("langVietNam");
            ImJP.ToolTip = rm.GetString("langJapan");
            linkBtnLogout.OnClientClick = "return confirm('" + rm.GetString("langConfirmLogout") + "');";
            lblWebName.Text = rm.GetString("langWebName");
            lblFooter1.Text = rm.GetString("langFooter1");
            lblFooter2.Text = rm.GetString("langFooter2");
        }

        /// <summary>
        /// Load page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //ChangeActivePage();

            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["userID"] == null)
            {
                linkBtnLogin.Visible = true;
                linkBtnRegister.Visible = true;
                tbxEmail.Visible = true;
                tbxPassword.Visible = true;
                linkBtnAccount.Visible = false;
                linkBtnLogout.Visible = false;

                linkBtnAccount.Text = "Account";
                linkBtnAccount.Font.Bold = false;
                linkBtnAccount.Style.Value = "margin-left: 10px; color: #ADD8E6;";

                TabWarehouse.Visible = false;
                TabTrading.Visible = false;
                ImWarehouse.Visible = false;
                ImTrading.Visible = false;
            }
            else
            {
                linkBtnLogin.Visible = false;
                linkBtnRegister.Visible = false;
                tbxEmail.Visible = false;
                tbxPassword.Visible = false;
                linkBtnAccount.Visible = true;
                linkBtnLogout.Visible = true;

                linkBtnAccount.Text = Session["userName"].ToString();
                linkBtnAccount.Font.Bold = true;
                linkBtnAccount.Style.Value = "color: #ADD8E6;";

                TabWarehouse.Visible = true;
                TabTrading.Visible = true;
                ImWarehouse.Visible = true;
                ImTrading.Visible = true;
            }
        }

        private void ChangeActivePage()
        {
            
        }

        /// <summary>
        /// On click LinkButton Login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkBtnLogin_Click(object sender, EventArgs e)
        {
            BOLAccount bolAcc = new BOLAccount();
            User user = new User();

            // Check email and password input
            if (tbxEmail.Text.Trim().Equals("") && tbxPassword.Text.Trim().Equals(""))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_INPUT_EMAIL_AND_PASSWORD) + "')</script>", false);
                return;
            }

            if (tbxEmail.Text.Trim().Equals(""))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_INPUT_EMAIL) + "')</script>", false);
                return;
            }

            if (tbxPassword.Text.Trim().Equals(""))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_INPUT_PASSWORD) + "')</script>", false);
                return;
            }

            // Perform & Check login: If login success
            string message = "";
            string password = tbxPassword.Text.Trim();
            password = PRLCommon.Encrypt(password, true);
            if (bolAcc.login(tbxEmail.Text.Trim(), password, out user, out message))
            {
                Session["userID"] = user.ID;
                Session["userName"] = user.Name;
                Response.Redirect("~/PRL.Trading/TransactionPage.aspx");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
                tbxPassword.Text = "";
            }
        }

        /// <summary>
        /// On click LinkButton Register
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkBtnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/PRL.Account/RegisterPage.aspx");
        }

        /// <summary>
        /// On click LinkButton Account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkBtnAccount_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/PRL.Account/AccountPage.aspx");
        }

        /// <summary>
        /// On click LinkButton Logout
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkBtnLogout_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            //langWarehouse.Visible = false;
            //langTrading.Visible = false;
            Response.Redirect("~/HomePage.aspx");
        }

        protected void ImVN_Click(object sender, ImageClickEventArgs e)
        {
            Session["language"] = "vi";
            Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
            UpdateUI();
            Response.Redirect(Request.RawUrl);
        }

        protected void ImJP_Click(object sender, ImageClickEventArgs e)
        {
            Session["language"] = "jp";
            Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
            UpdateUI();
            Response.Redirect(Request.RawUrl);
        }

    }
}
